package com.huawei.Galenus.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.Galenus.domain.Auth;
import com.huawei.Galenus.domain.Auth1;
import com.huawei.Galenus.domain.Message;
import com.huawei.Galenus.service.AuthService;
import com.huawei.Galenus.service.FabricService;
import com.huawei.Galenus.service.SongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/auth")
@Api("授权相关接口")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private FabricService fabricService;

    @Autowired
    private SongService songService;

    @ApiOperation("申请购买授权")
    @PostMapping("/buy")
    public Message buy(@ApiParam("购买者的用户ID") @RequestParam("uid")String uid,
                       @ApiParam("歌曲结构体里的creator")@RequestParam("creator") String creator,
                       @RequestBody Auth auth) {
        String id = UUID.randomUUID().toString();
        auth.setId(id);
        auth.setStatus(0);
        auth.setCreator(creator);
        int flag = authService.save(auth);
        if (flag == 1) {
            return new Message().ok(6666, "申请购买授权成功");
        } else {
            return new Message().error(1111, "申请购买授权失败");
        }
    }

    @ApiOperation("查看我的所有授权")
    @GetMapping("/all")
    public Message getAll(@RequestParam("uid") String uid) {
        List<Auth> auths = authService.getMyAuth(uid);
        return new Message().ok(6666, "获取成功").addData("auths", auths);
    }

    @ApiOperation("同意授权")
    @PutMapping("/approve")
    public Message approve(@ApiParam("授权ID") @RequestParam("aid") String aid) {
        Auth auth = authService.getOne(aid);
        Auth1 auth1 = new Auth1();
        auth1.setId(auth.getId());
        auth1.setPid(auth.getPid());
        auth1.setAuthtime(auth.getAuthtime());
        auth1.setAuthfilehash(auth.getAuthfilehash());
        auth1.setAuthsign(auth.getAuthsign());
        auth1.setHash(auth.getHash());
        auth1.setPrice(auth.getPrice());
        auth1.setDescription(auth.getDescription());

        JSONObject jsonObject = (JSONObject) JSON.toJSON(auth1);
        String[] params = new String[]{auth.getUid(), jsonObject.toJSONString()};
        String method = "bpsong";
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            authService.approve(aid, 1);
            return new Message().ok(6666, "授权成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "授权失败").addData("payload", payload);
        }
    }

    @ApiOperation("拒绝授权")
    @PutMapping("/reject")
    public Message reject(@ApiParam("授权ID") @RequestParam("aid") String aid) {
        int flag = authService.approve(aid, 2);
        if (flag == 1) {
            return new Message().ok(6666, "授权成功");
        } else {
            return new Message().ok(1111, "授权失败");
        }
    }

    @ApiOperation("查看我购买的授权")
    @GetMapping("/my_bought")
    public Message getMyBoughtAuth(@ApiParam("用户ID") @RequestParam("uid") String uid) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "bqauth";
        JSONObject payload = fabricService.songInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "获取成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "获取失败").addData("payload", payload);
        }
    }

}
